package com.ruoyi.merchant.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.Pattern;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.security.annotation.InnerAuth;
import com.ruoyi.merchant.domain.dto.MerchantRankingDTO;
import com.ruoyi.merchant.mapper.MerchantMapper;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.merchant.domain.Merchant;
import com.ruoyi.merchant.service.IMerchantService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 商户管理Controller
 * 
 * @author ruoyi
 * @date 2025-07-09
 */
@RestController
@RequestMapping("/merchant")
public class MerchantController extends BaseController
{
    @Autowired
    private IMerchantService merchantService;

    @Autowired
    private MerchantMapper merchantMapper;

    /**
     * 获取商户详细信息
     */
    @RequiresPermissions("merchant:merchant:query")
    @GetMapping(value = "/{mId}")
    public AjaxResult getInfo(@PathVariable("mId") Long mId)
    {
        return success(merchantService.selectMerchantByMId(mId));
    }

    /**
     * 查询所有的商户列表
     */
    @RequiresPermissions("merchant:merchant:list")
    @GetMapping("/list")
    public TableDataInfo list(Merchant merchant)
    {
        startPage();
        List<Merchant> list = merchantService.selectMerchantList(merchant);
        System.out.println(list);
        return getDataTable(list);
    }

    /**
     * 查询未被删除的商户列表
     *
     * @return
     */
    @RequiresPermissions("merchant:merchant:list")
    @GetMapping("/selectNotDeleteMerchants")
    public TableDataInfo selectNotDeleteMerchants(Merchant merchant) {
        startPage();
        List<Merchant> list = merchantService.selectNotDeleteMerchants(merchant);
        return getDataTable(list);
    }

    /**
     * 查询审核已被拒绝的商户列表
     *
     * @return
     */
    @RequiresPermissions("merchant:merchant:list")
    @GetMapping("/selectRefuseAuditStatus")
    public TableDataInfo selectRefuseAuditStatus(Merchant merchant) {
        startPage();
        List<Merchant> list = merchantService.selectRefuseAuditStatus(merchant);
        return getDataTable(list);
    }

    /**
     * 查询已通过审核的商户列表
     */
    @RequiresPermissions("merchant:merchant:list")
    @GetMapping("/PassedMerchantsList")
    public TableDataInfo PassedMerchantsList(Merchant merchant) {
        startPage();
        List<Merchant> list = merchantService.selectPassedMerchantList(merchant);
        return getDataTable(list);
    }

    /**
     * 查询已审核且未被删除的商户列表
     *
     * @return
     */
    @RequiresPermissions("merchant:merchant:list")
    @GetMapping("/selectPassedAndNotDelMerchantList")
    public TableDataInfo selectPassedAndNotDelMerchantList(Merchant merchant) {
        startPage();
        List<Merchant> list = merchantService.selectPassedAndNotDelMerchantList(merchant);
        return getDataTable(list);
    }

    /**
     * 查询正在营业的商户列表(已审核且未删除)
     *
     * @return
     */
    @RequiresPermissions("merchant:merchant:list")
    @GetMapping("/selectOpeningMerchantList")
    public TableDataInfo selectOpeningMerchantList(Merchant merchant) {
        startPage();
        List<Merchant> list = merchantService.selectOpeningMerchantList(merchant);
        return getDataTable(list);
    }

    /**
     * 查询已通过审核的商户列表并根据评论星级排名
     */
    @RequiresPermissions("merchant:merchant:list")
    @GetMapping("/PassedMerchantsListRanking")
    public TableDataInfo PassedMerchantsListRanking()
    {
        startPage();
        List<MerchantRankingDTO> list = merchantService.PassedMerchantsListRanking();
        return getDataTable(list);
    }

    /**
     * 导出商户管理列表
     */
    @RequiresPermissions("merchant:merchant:export")
    @Log(title = "商户管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Merchant merchant)
    {
        List<Merchant> list = merchantService.selectMerchantList(merchant);
        ExcelUtil<Merchant> util = new ExcelUtil<Merchant>(Merchant.class);
        util.exportExcel(response, list, "商户管理数据");
    }

    /**
     * 新增商户管理
     */
    @RequiresPermissions("merchant:merchant:add")
    @Log(title = "商户管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Merchant merchant)
    {
        return toAjax(merchantService.insertMerchant(merchant));
    }

    /**
     * 商户注册接口
     *
     * @param merchant
     * @param verificationCode
     * @return
     */
    @PostMapping("/registerMerchant")
    public AjaxResult registerMerchant(@RequestBody Merchant merchant, @RequestParam("verificationCode") int verificationCode) {
        System.out.println(merchant);
        return merchantService.registerMerchant(merchant, verificationCode);
    }

    /**
     * 注册时根据手机号获取验证码接口
     *
     * @param phoneNumbers
     * @return
     */
    @GetMapping("/getVerificationCodeRegister")
    public AjaxResult getVerificationCodeRegister(String[] phoneNumbers) {
        return merchantService.getVerificationCodeRegister(phoneNumbers);
    }

    /**
     * 商户登录接口
     *
     * @param phoneNumber
     * @return
     */
    @PostMapping("/loginMerchant")
    public AjaxResult loginMerchant(String phoneNumber) {
        return merchantService.loginMerchant(phoneNumber);
    }

    /**
     * 登录时根据手机号获取验证码接口
     * @param phoneNumbers
     * @return
     */
    @GetMapping("/getVerificationCodeLogin")
    public AjaxResult getVerificationCodeLogin(String[] phoneNumbers) {
        return merchantService.getVerificationCodeLogin(phoneNumbers);
    }

    /**
     * 修改商户管理
     */
    @RequiresPermissions("merchant:merchant:edit")
    @Log(title = "商户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Merchant merchant)
    {
        System.out.println(merchant);
        return toAjax(merchantService.updateMerchant(merchant));
    }

    /**
     * 商户审核通过时修改审核状态并将商户的信息注册到用户表中
     */
    @PutMapping("/auditAndRegister/{mId}")
    public AjaxResult auditAndRegister(@PathVariable("mId") Long mId) {
        return success(merchantService.auditAndRegister(mId));
    }

    /**
     * 删除商户管理
     */
    @RequiresPermissions("merchant:merchant:remove")
    @Log(title = "商户管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{mIds}")
    public AjaxResult remove(@PathVariable Long[] mIds)
    {
        return toAjax(merchantService.deleteMerchantByMIds(mIds));
    }
}
